package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.MyQuestionAdapter;
import com.kupurui.asteacher.bean.MyQuestionInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.util.PtrInitHelper;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/8/30.
 */

public class MyQuestionBankAty extends BaseAty implements PtrHandler, LoadMoreHandler {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.tv_empty)
    TextView tvEmpty;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    @Bind(R.id.fbtn_preview)
    FButton fbtnPreview;

    MyQuestionAdapter adapter;
    List<MyQuestionInfo> list;
    private int page = 1;

    MyQuestionInfo item;//临时点击的item

    @Override
    public int getLayoutId() {
        return R.layout.my_question_bank_aty;
    }

    @Override
    public void initData() {
        list = new ArrayList<>();

        PtrInitHelper.initPtr(this, ptrFrame);
        ptrFrame.setPtrHandler(this);
        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(true);
        loadMoreListViewContainer.loadMoreFinish(false, true);
        loadMoreListViewContainer.setLoadMoreHandler(this);
        listview.setFooterDividersEnabled(false);

        adapter = new MyQuestionAdapter(this,list,R.layout.job_sel_item);
        listview.setAdapter(adapter);

        fbtnPreview.setText("已选"+ UserManger.subject_id.size()+"道题，点击预览");
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().singleTiku(UserManger.getId(),page+"",this,0);
    }

    @Override
    public void intoActivity(Object object, int what) {
        if (what == 0){
            item = (MyQuestionInfo) object;
            fbtnPreview.setText("已选"+ UserManger.subject_id.size()+"道题，点击预览");
        }
        super.intoActivity(object, what);
    }

    @OnClick({R.id.iv_back,R.id.fbtn_preview})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.fbtn_preview:
                if (Toolkit.listIsEmpty(UserManger.subject_id)){
                    showToast("请选择题目");
                    return;
                }
                startActivity(PreviewAty.class,null);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                list.clear();
                list.addAll(AppJsonUtil.getArrayList(result,MyQuestionInfo.class));
                adapter.notifyDataSetChanged();
                ptrFrame.refreshComplete();
                loadMoreListViewContainer.loadMoreFinish(false, true);

                for (int i = 0;i<list.size();i++){
                    if (UserManger.subject_id.contains(list.get(i).getId())) {
                        list.get(i).setIs_selector(true);
                    }
                }
                break;
            case 1:
                if (Toolkit.listIsEmpty(AppJsonUtil.getArrayList(result,MyQuestionInfo.class))){
                    loadMoreListViewContainer.loadMoreFinish(false, false);
                }else{
                    page++;
                    list.addAll(AppJsonUtil.getArrayList(result,MyQuestionInfo.class));
                    adapter.notifyDataSetChanged();
                    loadMoreListViewContainer.loadMoreFinish(false, true);
                }
                for (int i = 0;i<list.size();i++){
                    if (UserManger.subject_id.contains(list.get(i).getId())) {
                        list.get(i).setIs_selector(true);
                    }
                }
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(ptrFrame, listview, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        page = 1;
        new Teacher().singleTiku(UserManger.getId(),page+"",this,0);
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        new Teacher().singleTiku(UserManger.getId(),(page+1)+"",this,1);
    }
}
